<?php
namespace app\shop\controller;
use app\ApiRest;
use app\card\model\CardCouponRecord;
use app\card\model\Config;
use app\diy\model\DiyModel;
use app\dynamic\model\CardCompany;
use app\integral\info\PermissionIntegral;
use app\integral\model\Integral;
use app\member\info\PermissionMember;
use app\member\model\DiscountGoods;
use app\member\model\Goods;
use app\member\model\Member;
use app\shop\model\AdminGoodsParameterValue;
use app\shop\model\BargainAtv;
use app\shop\model\BargainConfig;
use app\shop\model\GoodsWholesale;
use app\Test;
use think\App;
use think\facade\Cache;
use think\facade\Db;
use think\Request;
use app\shop\model\IndexGoods as GoodsModel;
use app\shop\model\IndexShopType;
use app\shop\model\IndexUserShop;
use app\shop\model\IndexCompany;
use app\shop\model\IndexCoupon;
use app\shop\model\IndexCouponRecord;
use app\shop\model\IndexShopSpe;
use app\shop\model\IndexShopSpePrice;
use app\shop\model\IndexUserInfo;
use app\shop\model\IndexShopCar;
use app\shop\model\IndexShopCollageList;
use app\shop\model\IndexExtension;
use think\route\Rule;


class IndexShopGoods extends ApiRest
{

//    protected $noNeedLogin = ['goodsInfo'];

    protected $model;
    protected $spe_model;
    protected $spe_price_model;
    protected $shop_type_model;
    protected $user_shop;
    protected $company;
    protected $coupon;
    protected $coupon_record;
    protected $user_info_model;
    protected $car_model;
    protected $collage_list;
    protected $extension;

    protected $member_model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model           = new GoodsModel();
        $this->shop_type_model = new IndexShopType();
        $this->user_shop       = new IndexUserShop();
        $this->company         = new IndexCompany();
        $this->coupon          = new IndexCoupon();
        $this->coupon_record   = new IndexCouponRecord();
        $this->spe_model       = new IndexShopSpe();
        $this->spe_price_model = new IndexShopSpePrice();
        $this->user_info_model = new IndexUserInfo();
        $this->car_model       = new IndexShopCar();
        $this->collage_list    = new IndexShopCollageList();
        $this->extension       = new IndexExtension();
        $this->goods_parameter = new AdminGoodsParameterValue();

        $this->member_model    = new Member();

        $admin_order = new IndexCheck($app);

        $admin_order->checkOrder();
    }
    /**
     * 获取商品列表
     */
    public function goodsList(){

        $input      = $this->_param;

        $to_uid     = $input['staff_id'];

        if(lbIsStaff($to_uid)!=1){

            return $this->error('not find card');
        }
        $user_id    = $this->getUserId();

        $to_uid     = !empty($input['staff_id'])?$input['staff_id']:$this->getUserId();

        $company_id = $this->user_info_model->getCompanyId(['fans_id'=>$to_uid],$this->_uniacid);

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if($this->member_model->getAuth($this->_uniacid)==true) {

            $dis[] = ['a.is_member', '=', 0];
        }

        if(!empty($input['type'])){

            $all_cat = $this->shop_type_model->getSonCatId($input['type']);

            $dis[]   = ['type','in',$all_cat];
        }

        $dis[] = ['a.status' ,'=',1];
        //商品名字搜索
        if(!empty($input['name'])){

            $dis[] = ['a.name','like',"%".$input['name']."%"];
        }
        //自选
        if($this->_config['myshop_switch']==1){

            $my_goods = $this->user_shop->MyGoodList(['user_id'=>$to_uid,'uniacid'=>$this->_uniacid]);

            $dis[] = ['a.id','in',$my_goods];

        }else{

            $dis[] = ['a.index_show', '=', 1];

        }
        //公共商品
        $where[] = ['a.public_goods','=',1];
        //在这个公司下面
        $where[] = ['b.company_id' ,'=',$company_id];

        $data['goods'] = $this->model->goodListV2($dis,$to_uid,10,$where);

        $data['type']  = $this->shop_type_model->catSelect(['uniacid'=>$this->_uniacid,'status'=>1]);

        $top_company   = $this->company->topCompany($company_id);
        //By.jingshuixian
        if(!empty($top_company)){
            $data['banner']= $this->user_shop->getShopBanner(['c_id'=>$top_company['id'],'status'=>1,'uniacid'=>$this->_uniacid]);

        }

        $data['comany']= $this->company->companyInfo($to_uid,$this->_uniacid);

        if(empty($data['banner'])){
            if(!empty($top_company['desc'])){
                $data_no_banner['img']   = [$top_company['desc']];
                $data_no_banner['cover'] = $top_company['desc'];
//                $data['banner'][]['img'][]   = $top_company['desc'];
//                $data['banner'][]['cover'] = $top_company['desc'];
                $data['banner'][] = $data_no_banner;

            }
        }
        $data['config']= $this->_config;
        $coupon_dis    = ['status'=>1,'uniacid'=>$this->_uniacid];
        $data['coupon']= $this->coupon->couponListSelect($coupon_dis,$user_id,$to_uid);
        $data['car']   = $this->car_model->carCount(['uniacid'=>$this->_uniacid,'user_id'=>$user_id,'status'=>1]);


        $dis = [

            'uniacid' => $this->_uniacid
        ];

        $model = new BargainConfig();

        $data['shop_share_status'] = $model->where($dis)->value('shop_share_status');

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-29 17:20
     * @功能说明:会员商品列表
     */
    public function memberGoodsList(){

        $input  = $this->_param;

        $page   = 10;

        $to_uid = $input['staff_id'];

        if(lbIsStaff($to_uid)!=1){

            return $this->error('not find card');
        }
        //公司id
        $company_id = $this->user_info_model->getCompanyId(['fans_id'=>$to_uid],$this->_uniacid);

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.status' ,'=',1];

        $dis[] = ['a.is_member' ,'=',1];
        //商品名字搜索
        if(!empty($input['name'])){

            $dis[] = ['a.name','like',"%".$input['name']."%"];
        }
        //会员商品模型
        $member_goods_model = new Goods();
        //可买的会员商品id
        $member_goods = $member_goods_model->getCanBuyGoods($this->getUserId(),$this->_uniacid);
        //自选
        if($this->_config['myshop_switch']==1){

            $my_goods = $this->user_shop->MyGoodList(['user_id'=>$to_uid,'uniacid'=>$this->_uniacid]);
            //获取交集
            $member_goods = array_intersect($member_goods,$my_goods);

        }

        $dis[] = ['a.id','in',$member_goods];
        //公共商品
        $where[] = ['a.public_goods','=',1];
        //在这个公司下面
        $where[] = ['b.company_id' ,'=',$company_id];
        //查询
        $data    = $this->model->goodListV2($dis,$to_uid,$page,$where);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){
                //等级信息
                $level = $member_goods_model->getGoodsLevel($v['id']);
                //等级名称
                $v['level_title'] = !empty($level['title'])?$level['title']:'';

                $o_dis = [

                    'status' => 1,

                    'goods_id' => $v['id']
                ];

                $max_price = $this->spe_price_model->where($o_dis)->max('price');

                $v['area_price'] = $v['price_text'] != $max_price? $v['price_text'].'~'.$max_price:$v['price_text'];

            }

        }

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2020-06-11 14:59
     * @功能说明:商城首页diy版本
     */
    public function goodsListV2(){
        //获取diy的数据
        $diy_data = DiyModel::where([['status', '=', 1],['uniacid', '=', $this->_uniacid]])->find();
        //获取商城的
        $page_data= json_decode($diy_data['page'], true)[2] ?? [];
        //判断有没有diy过
        if(empty($page_data)|| (array_key_exists('list',$page_data) && empty($page_data['list']))){

            $page_data = longbing_default_Page(2);

        }
        //获取参数
        $input  = $this->_param;
        //名片id
        $to_uid = $input['staff_id'];
        //查看是否是是员工
        if(lbIsStaff($to_uid)!=1){

            return $this->error('not find card');
        }
        //用户id
        $user_id    = $this->getUserId();
        //公司id
        $company_id = $this->user_info_model->getCompanyId(['fans_id'=>$to_uid],$this->_uniacid);
        //获取数据
        foreach ($page_data['list'] as &$value){

            //获取banner
            if($value['type']=='banner'&&empty($value['data']['bannerList'])){
                //顶级公司
                $top_company   = $this->company->topCompany($company_id);

                if(!empty($top_company)){

                    $value['data']['bannerList']= $this->user_shop->shopBannerList(['c_id'=>$top_company['id'],'status'=>1,'uniacid'=>$this->_uniacid],'img');
                }
                if(empty($value['data']['bannerList'])){

                    if(!empty($top_company['desc'])){

                        $data_no_banner['img'][0]['url'] = $top_company['desc'];

                        $data_no_banner['linkType'] = 4;

                        $value['data']['bannerList'][] = $data_no_banner;

                    }
                }
            }
            //获取商品
            if($value['type']=='goodsList'&&empty($value['data']['dataList'])){

                $dis[] = ['a.uniacid','=',$this->_uniacid];

                if($this->member_model->getAuth($this->_uniacid)==true) {

                    $dis[] = ['a.is_member', '=', 0];
                }

                $dis[] = ['a.uniacid','=',$this->_uniacid];

                $dis[] = ['a.status' ,'=',1];
                //商品名字搜索
                if(!empty($input['name'])){

                    $dis[] = ['a.name','like',"%".$input['name']."%"];
                }
                //自选
                if($this->_config['myshop_switch']==1){

                    $my_goods = $this->user_shop->MyGoodList(['user_id'=>$to_uid,'uniacid'=>$this->_uniacid]);

                    $dis[] = ['a.id','in',$my_goods];

                }else{

                    $dis[] = ['a.index_show', '=', 1];
                }
                //公共商品
                $where[] = ['a.public_goods','=',1];
                //在这个公司下面
                $where[] = ['b.company_id' ,'=',$company_id];

                $value['data']['dataList']  = $this->model->goodListV2($dis,$to_uid,10,$where);
                //只返回一页
                $value['data']['dataList']['last_page'] = !empty($value['data']['limit'])?1:$value['data']['dataList']['last_page'];
            }

            //获取分类
            if($value['type']=='column'&&empty($value['data']['columnList'])) {

                $value['data']['columnList']  = $this->shop_type_model->catSelectDiy(['uniacid'=>$this->_uniacid,'status'=>1,'pid'=>0],'id,title,cover as img,pid');
            }

            //优惠券
            if($value['type']=='couponList'&&empty($value['data']['dataList'])) {

                $coupon_dis = ['status'=>1,'uniacid'=>$this->_uniacid];

                $dataList   = $this->coupon->couponListSelect($coupon_dis,$user_id,$to_uid);

                if(!empty($dataList)){
                    //列表样式
                    if($value['data']['type']==2){

                        $value['data']['dataList'] = $dataList;

                    }else{
                        //弹窗样式
                        $value['data']['dataList']['coupon'] = $dataList[0];
                        //领取记录
                        $r_dis = [
                            //优惠券id
                            'coupon_id' => $dataList[0]['id'],
                            //员工
                            'staff_id'  => $to_uid,
                            //uniacid
                            'uniacid'   => $this->_uniacid
                        ];

                        $value['data']['dataList']['coupon_last_record'] = $this->coupon_record->where($r_dis)->limit(3)->order('id desc')->select()->toArray();

                        if ( !empty(  $value['data']['dataList']['coupon_last_record'] ) )
                        {

                            foreach (  $value['data']['dataList']['coupon_last_record'] as $index => $item )
                            {
//
                                $user = longbingGetUser($item[ 'user_id' ] ,$this->_uniacid);

                                if ( mb_strlen( $user[ 'nickName' ], 'utf8' ) > 4 )
                                {
                                    $user[ 'nickName' ] = mb_substr( $user[ 'nickName' ], 0, 4, "UTF-8" );
                                }

                                $value['data']['dataList']['coupon_last_record'][ $index ][ 'user_info' ] = $user;
                            }

                        }
                    }
                }
            }
        }

        $page_data['car']   = $this->car_model->carCount(['uniacid'=>$this->_uniacid,'user_id'=>$user_id,'status'=>1]);

        $dis = [

            'uniacid' => $this->_uniacid
        ];

        $model = new BargainConfig();

        $share_data = $model->dataInfo($dis);

        $page_data['shop_share_status'] = $share_data['shop_share_status'];

        $page_data['shop_share_img']    = $share_data['shop_share_img'];

        return $this->success($page_data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-06-12 16:33
     * @功能说明:获取下级的分类
     */
    public function getSonCate(){

        $input = $this->_param;

        $data  = $this->shop_type_model->catSelectDiy(['uniacid'=>$this->_uniacid,'status'=>1,'pid'=>$input['id']],'id,title,cover as img,pid');

        return $this->success($data);

    }


    /**
     *
     * 获取商城分页
     */
    public function goodsPageList(){

        $input      = $this->_param;

        if(!empty($input['staff_id'])&&lbIsStaff($input['staff_id'])!=1){

            return $this->error('not find card');
        }

        $to_uid     = !empty($input['staff_id'])?$input['staff_id']:$this->getUserId();

        $company_id = $this->user_info_model->getCompanyId(['fans_id'=>$to_uid],$this->_uniacid);

        $dis[] = ['a.uniacid','=',$this->_uniacid];

//        if(!empty($input['index_show'])){
//
//            $dis[] = ['a.index_show', '=', 1];
//        }

        if($this->member_model->getAuth($this->_uniacid)==true) {

            $dis[] = ['a.is_member', '=', 0];
        }

        if(!empty($input['type'])){

            $all_cat = $this->shop_type_model->getSonCatId($input['type']);

            $dis[]   = ['type','in',$all_cat];
        }

        $dis[] = ['a.status' ,'=',1];
        //商品名字搜索
        if(!empty($input['name'])){

            $dis[] = ['a.name','like',"%".$input['name']."%"];

        }
        //自选
        if($this->_config['myshop_switch']==1&&!empty($input['staff_id'])){

            $my_goods = $this->user_shop->MyGoodList(['user_id'=>$to_uid,'uniacid'=>$this->_uniacid]);

            $dis[] = ['a.id','in',$my_goods];

        }

        if($this->_config['myshop_switch']==0&&!empty($input['index_show'])&&!empty($input['staff_id'])){

            $dis[] = ['a.index_show', '=', 1];
        }
        //公共商品
        $where[] = ['a.public_goods','=',1];
        //在这个公司下面
        $where[] = ['b.company_id' ,'=',$company_id];

        $data  = $this->model->goodListV2($dis,$to_uid,10,$where);

        $car_model = new IndexShopCar();
        //查询每一件商品在购物车里面的数量
        if(!empty($data['data'])){

            foreach ($data['data'] as $key=>$value){

                $where = [

                    'user_id' => $this->getUserId(),

                    'goods_id'=> $value['id'],

                    'status'  => 1
                ];

                $data['data'][$key]['car_count'] = $car_model->where($where)->sum('number');

            }
        }

        if(empty($input['staff_id'])){

            $my_goods  = $this->user_shop->MyGoodList(['user_id'=>$to_uid,'uniacid'=>$this->_uniacid]);

            if(!empty($data['data'])){

                foreach ($data['data'] as &$v){

                    $v['is_my_goods']  = in_array($v['id'],$my_goods)?1:0;
                }
            }

            $data['type']  = $this->shop_type_model->catSelect(['uniacid'=>$this->_uniacid,'status'=>1]);

            $data['myshop_switch'] = $this->_config['myshop_switch'];
        }


        return $this->success($data);
    }



    /**
     * @author chenniang
     * @DataTime: 2021-02-03 11:15
     * @功能说明:砍价活动列表
     */
    public function bargainList(){

        $input      = $this->_param;

        $to_uid     = !empty($input['staff_id'])?$input['staff_id']:$this->getUserId();

        $company_id = $this->user_info_model->getCompanyId(['fans_id'=>$to_uid],$this->_uniacid);

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if($this->member_model->getAuth($this->_uniacid)==true) {

            $dis[] = ['a.is_member', '=', 0];
        }
        //自选
        if($this->_config['myshop_switch']==1&&!empty($input['staff_id'])){

            $my_goods = $this->user_shop->MyGoodList(['user_id'=>$to_uid,'uniacid'=>$this->_uniacid]);

            $dis[] = ['a.id','in',$my_goods];

        }
        $dis[]   = ['e.start_time','<',time()];

        $dis[]   = ['e.end_time','>',time()];

        $dis[]   = ['e.status','=',1];
        //公共商品
        $where[] = ['a.public_goods','=',1];
        //在这个公司下面
        $where[] = ['b.company_id' ,'=',$company_id];
        //砍价活动
        $bargan_atv_model   = new BargainAtv();

        $data  = $bargan_atv_model->atvIngList($dis,$where);

        return $this->success($data);

    }

    /**
     * User: chenniang
     * Date: 2019-09-09 11:33
     * @return \think\Response
     * descption: 分类列表
     *
     */
    public function getCateList(){
        $page  = 10;
        $dis[] = ['uniacid','=',$this->_uniacid];
        $dis[] = ['status' ,'=',1];
        $data  = $this->shop_type_model->getCatList($dis,$page);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-09-09 11:33
     * @return \think\Response
     * descption: 商品详情
     */
    public function goodsInfo(){

        $input = $this->_param;

        $dis[] = ['id','=',$input['id']];

        $dis[] = ['status','=',1];

        $data  = $this->model->goodsInfo($dis);

        if(empty($data)){

            return $this->error('商品未找到');
        }

        if(lbIsStaff($input['staff_id'])!=1){

            return $this->error('not find card');
        }
        $this->model->goodsUpdate(['id'=>$input['id']],['view_count'=>Db::raw('view_count+1')]);

        $data    = transImages($data,['cover','images'],',');

        $data    = transImagesOne($data,['video']);

        $data['video_vid'] = !empty($data['video'])?lbGetTencentVideo($data['video']):'';

        $data    = transImagesOne($data,['video_cover']);

        $where[] = ['goods_id','=',$input['id']];

        $where[] = ['status'  ,'=',1];
        //老版
        if($input['staff_id']==$this->getUserId()&&$data['staff_switch']!=0){

            $data['extract'] = !$data[ 'staff_extract' ] || $data[ 'staff_extract' ] < 1 || $data[ 'staff_extract' ] > 100?$this->_config[ 'staff_extract' ]: $data[ 'staff_extract' ];

        }elseif($input['staff_id']!=$this->getUserId()&&$data['switch']!=0){

            $data['extract'] = !$data[ 'extract' ] || $data[ 'extract' ] < 1 || $data[ 'extract' ] > 100?$this->_config[ 'first_extract' ]: $data[ 'extract' ];

        }else{

            unset($data['extract']);
        }
        //新版
        if($input['staff_id']!=$this->getUserId()&&$data['switch']!=0){
            if($data['switch']==2){

                $data['new_extract'] = $data['extract_cash'].'¥';

            }elseif($data['switch']==1){

                $data['new_extract'] = $data['extract'].'%';
            }else{


                if($this->_config['selling_style']==0){

                    $data['new_extract'] = 0;

                }elseif ($this->_config['selling_style']==1){

                    $data['new_extract'] = $this->_config['first_extract']."%";

                }elseif ($this->_config['selling_style']==2){

                    $data['new_extract'] = $this->_config['first_extract_cash'].'¥';
                }

            }

        }elseif($input['staff_id']==$this->getUserId()&&$data['staff_switch']!=0){

            if($data['staff_switch']==2){

                $data['new_extract'] = $data['staff_extract_cash'].'¥';

            }elseif($data['staff_switch']==1){

                $data['new_extract'] = $data['staff_extract'].'%';
            }else{

                if($this->_config['selling_style']==0){

                    $data['new_extract'] = 0;

                }elseif ($this->_config['selling_style']==1){

                    $data['new_extract'] = $this->_config['staff_extract']."%";

                }elseif ($this->_config['selling_style']==2){

                    $data['new_extract'] = $this->_config['staff_extract_cash'].'¥';
                }

            }

        }else{
            $data['new_extract'] = 0;
        }
        //是否是拼团商品
        $data['is_collage']  = empty($data['collage_text'])?0:1;

        $data['extract']     = !empty($data['extract'])?$data['extract']:0;

        $data['spe_text']    = $this->spe_model->goodsSpe($where);

        $data['spe_price']   = $this->spe_price_model->goodsSpePrice($where);

        $collage_overtime    = !empty($this->_config['collage_overtime'])?$this->_config['collage_overtime']:3600;

        $collage_list = $this->collage_list->userCollageList(['a.goods_id'=>$input['id'],'a.uniacid'=>$this->_uniacid],$this->getUserId(),$collage_overtime,2);

        $data['collage_list']  = $collage_list['data'];

        $data['collage_count'] = $collage_list['total'];

        $userinfo     = new IndexUserInfo();

        $card_company = new CardCompany();

        $company_id   = $userinfo->getCompanyId(['fans_id'=>$input['staff_id']],$this->_uniacid);
        //找到公司最上级
        $company_id = $card_company->getUserTopCompanyId($company_id);

        $company    = $card_company->getinfo(['uniacid'=>$this->_uniacid,'id'=>$company_id]);

        $data['company']  = $company['name'];

        $data['car']      = $this->car_model->carCount(['uniacid'=>$this->_uniacid,'user_id'=>$this->getUserId(),'status'=>1]);
        //By.jingshuixian  兼容新的小程序html解析工具 需要优化
        $data['content1'] = getimgsV2($data['content']);
        //商城分销设置查询是否前台展示分销金额
        $setting = new \app\shop\model\AdminShopSetting();

        $data['commission_display'] = $setting->where('uniacid',$this->_uniacid)->value('commission_display');
        //会员模型
        $member_model  = new Member();
        //会员商品信息
        $data['member_info'] = $member_model->goodsInfoMember($input['id'],$this->getUserId(),$this->_uniacid);

        $max= [

            'status' => 1,

            'goods_id'=> $data['id']
        ];

        $data['max_original_price'] = Db::name('longbing_card_shop_spe_price')->where($max)->max('original_price');
        //砍价活动
        $bargan_atv_model   = new BargainAtv();
        //正在进行中的砍价活动
        $data['bargain_atv']    = $bargan_atv_model->atvIng($data['id']);
        //我的正在砍价的活动
        $data['my_bargain_atv'] = $bargan_atv_model->myRecordIng($data['id'],$this->getUserId());

        $wholesale_model = new GoodsWholesale();
        //批发价活动
        $data['wholesale'] = $wholesale_model->wholesaleAtv($data['id']);

        $must_atv_spe = $bargan_atv_model->mastAtvSpe($data['id']);
        //检查积分商城权限
        $i_p     = new PermissionIntegral($this->_uniacid);

        $i_model = new Integral();

        if($i_p==true){

            $i_model->initAtv();
            //正在进行中的积分活动活动
            $data['integral_atv'] = $i_model->atvIngList($input['id']);

        }

        foreach ($data['spe_price'] as &$v){

            $v['is_show'] = in_array($v['id'],$must_atv_spe)?0:1;

            if(!empty($data['integral_atv'])){

                foreach ($data['integral_atv'] as $i_v){

                    if($v['id']==$i_v['spe_id']){

                        $v['integral_atv'] = $i_v;

                        if($i_v['buy_limit']==0){

                            $v['is_show'] = 0;
                        }

                    }

                }

            }

        }

        return $this->success($data);
    }




    /**
     * @author chenniang
     * @DataTime: 2021-02-02 16:57
     * @功能说明:批发价活动的明细
     */
    public function wholesaleSelect(){

        $input = $this->_param;

        $wholesale_model = new GoodsWholesale();
        //批发价活动
        $data = $wholesale_model->wholesaleAtvSelect($input['goods_id']);

        return $this->success($data);

    }



    /**
     * User: chenniang
     * Date: 2019-09-09 11:33
     * @return \think\Response
     * descption:
     * 拼团列表
     */
    public function collageList(){

        $collage_overtime = !empty($this->_config['collage_overtime'])?$this->_config['collage_overtime']:3600;

        $input = $this->_param;

        $data  = $this->collage_list->userCollageList(['a.goods_id'=>$input['id'],'a.uniacid'=>$this->_uniacid],$this->getUserId(),$collage_overtime);

        return $this->success($data);
    }

    /**
     *
     * 拼团详情
     */

    public function collageInfo(){

        $collage_overtime = !empty($this->_config['collage_overtime'])?$this->_config['collage_overtime']:3600;

        $input = $this->_param;

        $data  = $this->collage_list->userCollageInfo(['a.id'=>$input['id'],'a.uniacid'=>$this->_uniacid],$this->getUserId(),$collage_overtime);
        //会员模型
        $member_model  = new Member();
        //会员商品信息
        $data['member_info'] = $member_model->goodsInfoMember($input['id'],$this->getUserId(),$this->_uniacid);


        return $this->success($data);
    }

    /**
     *
     * 获取福包列表
     */
    public function couponList(){
        $input = $this->_input;
        $dis[] = ['user_id','=',$input['user_id']];
        $dis[] = ['uniacid','=',$this->_uniacid];
        $dis[] = ['status' ,'=',$input['status']];
        $res   = $this->coupon_record->endRecord();
        $data  = $this->coupon_record->recordList($dis,$input['limit']);
        return $this->success($data);
    }


    /**
     *
     * 设置员工主推商品
     */

    public function doExtension(){
        $input = $this->_input;
        $dis   =  [
            'uniacid'  => $this->_uniacid,
            'user_id'  => $this->getUserId(),
            'goods_id' => $input['goods_id'],
        ];
        $extension = $this->extension->extensionInfo($dis);
        if(!empty($extension)){
            $this->extension->extensionDel($dis);
        }else{
            $this->doShopGoods($input['goods_id']);
            $inser_data = [
                'user_id' => $this->getUserId(),
                'uniacid' => $this->_uniacid,
                'goods_id'=> $input['goods_id']
            ];
            $this->extension->extensionAdd($inser_data);
        }
        return $this->success(true);
    }

    /**
     * @param $goods_id
     * 设置商城主推商品
     */

    public function doShopGoods($goods_id){
        $dis = [
            'uniacid'  => $this->_uniacid,
            'goods_id' => $goods_id,
            'user_id'  => $this->getUserId(),
            'status'   => 1
        ];
        $my_goods = $this->user_shop->userShopInfo($dis);

//        if($this->_config['myshop_switch']!=1){
//            $this->errorMsg('未开启我的商城');
//        }

        if(empty($my_goods)){
            $diss['a.uniacid'] = $this->_uniacid;
            $diss['a.status']  = 1;
            $diss['a.user_id'] = $this->getUserId();
            $shop_count      = $this->user_shop->userShopCount($diss);

            if($shop_count>=$this->_config['my_shop_limit']&&!empty($this->_config['my_shop_limit'])){
                $this->errorMsg('设置主推商品需要先将商品添加到我的商城, 我的商城数量达到上限, 无法继续添加');
            }

            $insertData = [
                'uniacid' => $this->_uniacid,
                'goods_id'=> $goods_id,
                'user_id' => $this->getUserId(),
                'status'  => 1
            ];
            $this->user_shop->userShopAdd($insertData);
        }
        return $my_goods;
    }


    /**
     *
     * 设置商城主推
     */
    public function doUserShop(){
        $input = $this->_input;
        $data  = $this->doShopGoods($input['goods_id']);
        if(!empty($data)){
            $this->user_shop->where(['id'=>$data['id']])->delete();
            $this->extension->extensionDel(['uniacid' => $this->_uniacid,'user_id' => $this->getUserId(),'goods_id' => $input['goods_id']]);
        }
        return $this->success(true);
    }


    /**
     *
     * 分销商品
     */

    public function sellingGoodsOld(){
        $input  = $this->_input;
        $where[]= ['a.status','=',1];
        $where[]= ['a.switch','<>',0];
//        $where[]= ['a.price','>',0];
        $where[]= ['a.uniacid','=',$this->_uniacid];
        $sort_text = [0=>'a.id desc',1=>'a.sale_count desc',2=>'min_price desc'];
        $sort = $sort_text[$input['type']];
        $data = $this->model->sellingGoods($where,$sort);
        $data['data'] = transImages($data['data'],['cover','images'],',');
        $data['data'] = formatNumberPrice($data['data'],['price']);
        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-23 11:58
     * @功能说明:分销商品
     */
    public function sellingGoods(){

        $input  = $this->_input;

        $dis = [];

        if(!empty($input['staff_id'])){

            $company_id = $this->user_info_model->getCompanyId(['fans_id'=>$input['staff_id']],$this->_uniacid);
            //公共商品
            $dis[] = ['a.public_goods','=',1];
            //在这个公司下面
            $dis[] = ['c.company_id' ,'=',$company_id];
        }

        $where[]= ['a.status','=',1];

        $where[]= ['a.switch','<>',0];

        $where[]= ['a.uniacid','=',$this->_uniacid];

        $sort_text = [0=>'a.id desc',1=>'a.sale_count desc',2=>'min_price desc'];

        $sort = $sort_text[$input['type']];

        $data = $this->model->sellingGoods($where,$sort,$dis);

        $data['data'] = transImages($data['data'],['cover','images'],',');

        $data['data'] = formatNumberPrice($data['data'],['price']);

        return $this->success($data);

    }


    /**
     * User: chenniang
     * Date: 2019-09-26 10:32
     * @return void
     * descption:生成商品二维码
     */
    public function getQr(){
        $input = $this->_input;
        $qr    = getCache($this->getUserId().'-'.$input['type'].'-'.$input['staff_id'].'-'.$input['id']."-qr",$this->_uniacid);
        if(empty($qr)){
            $user = $this->getUserInfo();
            $input['user_id'] = $this->getUserId();
            $input['pid']     = $user['pid'];
            $data = longbingCreateWxCode($this->_uniacid,$input,$input['page']);
            $data = transImagesOne($data ,['qr_path'] ,$this->_uniacid);
            $qr   = $data['qr_path'];
            setCache($this->getUserId().'-'.$input['type'].'-'.$input['staff_id'].'-'.$input['id']."-qr",$qr,3600,$this->_uniacid);
        }
        return $this->success($qr);
    }

    /**
     * @author lxj
     * @DataTime: 2020/5/19 17:17
     * @功能说明:获取商品参数
     */
    public function getGoodsParameter(){
        $input = $this->_param;
        $info = $this->goods_parameter->getHistory(['a.goods_id'=>$input['goods_id'],'a.status'=>1,'a.uniacid'=>$this->_uniacid],$input['goods_id']);
        return $this->success($info);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-08-21 10:12
     * @功能说明:获取顶级分类
     */
    public function topCate(){

        $dis = [

            'uniacid' => $this->_uniacid,

            'status'  => 1

        ];

        $data = $this->shop_type_model->catSelect($dis);

        return $this->success($data);
    }


}
